// 需要持久化存储的数据放入Profile
import 'package:app/models/user.dart';
import 'package:app/widgets/message.dart';
import 'package:flutter/material.dart';

ThemeMode? stringToThemeMode(String value) {
  for (ThemeMode mode in ThemeMode.values) {
    if (mode.toString() == value) {
      return mode;
    }
  }
  return null;
}

class Profile {
  String? token; // 密钥
  ThemeMode? themeMode; // 主题模式(深色、浅色、跟随系统)
  UserInfo? userInfo; // 用户信息

  Profile({this.token, this.themeMode, this.userInfo});

  Profile.fromJson(Map<String, dynamic> json) {
    if (json["token"] is String) {
      token = json["token"];
    }
    if (json["themeMode"] is String) {
      themeMode = stringToThemeMode(json["themeMode"]);
    }
    if (json["userInfo"] is Map<String, dynamic>) {
      userInfo = UserInfo.fromJson(json["userInfo"]);
    }
  }

  Map<String, dynamic> toJson() {
    final Map<String, dynamic> _data = <String, dynamic>{};
    _data["token"] = token;
    _data["themeMode"] = themeMode.toString();
    _data["userInfo"] = userInfo?.toJson();
    return _data;
  }
}
